package com.itheima.core.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.itheima.common.conversion.TrimConverter;
import com.itheima.core.pojo.product.Brand;
import com.itheima.core.service.product.BrandService;

import cn.itcast.common.page.Pagination;

/**
 * 品牌管理
 * 
 * @author zhang.cheng
 *
 *         2017年2月15日
 */
@Controller
public class BrandController {

	@Autowired
	private BrandService brandService;

	@RequestMapping("/brand/list.do")
	public String list(Integer pageNo, String name, Integer isDisplay, Model model) {
		TrimConverter trimConverter = new TrimConverter();
		String nametotrim = trimConverter.convert(name);

		// List<Brand> brands = brandService.selectBrandListByQuery(nametotrim,
		// isDisplay);
		// 分页查询
		final Pagination pagination = brandService.selectPaginationByQuery(pageNo, nametotrim, isDisplay);
		model.addAttribute("pagination", pagination);

		model.addAttribute("name", name);
		model.addAttribute("isDisplay", isDisplay);

		return "brand/list";
	}

	@RequestMapping(value = "/brand/toEditor.do")
	public String brandToEditor(Integer id, Model model) {
		Brand brand = brandService.selectBrandById(id);
		model.addAttribute("brand", brand);
		return "brand/edit";
	}

	@RequestMapping(value = "/brand/edit.do")
	public String edit(Brand brand, Model model) {
		System.out.println("保存图片的路径为：" + brand.getImgUrl());
		brandService.updateById(brand);
		return "redirect:/brand/list.do";
	}

	@RequestMapping(value = "/brand/deletes.do")
	public String toDeletes(Long[] ids) {
		int i = 0;
		for (Long id : ids) {
			i++;
			System.out.println("删除第"+i+"个商品的id为："+id);
		}
		
		brandService.deletes(ids);
		//跳转视图：重定向
		//return "redirect:/brand/list.do";
		return "forward:/brand/list.do";
	}

}
